/*
 * @Descripttion: 队列
 * @Author: liuWei37853264@qq.com
 * @Date: 2021-07-16 16:40:02
 * @LastEditors: liuWei
 * @LastEditTime: 2021-07-22 10:01:13
 */

interface queueType {
    enqueue: Function,
    denqueue: () => any,
    front: Function,
    isEmpty: () => boolean,
    size: () => number,
}
class Queue implements queueType {
    public items: any[]
    constructor() {
        this.items = []
    }

    /**
     * 入队向队列推入一个或多个
     * @param ele 
     */
    enqueue(...ele: any) {
        this.items.push(...ele)
    }

    /**
     * 移除队列头部元素
     * @returns {any} 被移除的项
     */
    denqueue(): any {
        return this.items.shift()
    }


    /**
     * 返回队列第一个元素
     * @returns {any}
     */
    front(): any {
        return this.items[0]
    }

    /**
     * 栈是否为空
     * @returns {boolean} 
     */
    isEmpty(): boolean {
        return this.items.length === 0
    }

    /**
     * 返回栈元素的个数
     * @returns {number}  
     */
    size(): number {
        return this.items.length
    }

}

interface PriorityQueueType {
    priorityEnqueue: Function
}

// 优先级队列
class PriorityQueue extends Queue implements PriorityQueueType {

    priorityEnqueue({ ele, order }: { ele: string, order: number }) {
        if (this.isEmpty()) {
            this.enqueue({ ele, order })
        }
        else {
            // 该flag表示是否符合循环内条件->即,即将插入的需要在前面
            // 如果最后的不符合直接插入至末尾
            let flag = false
            for (let i = 0; i < this.items.length; i++) {
                if (this.items[i].order >= order) {
                    this.items.splice(i, 0, { ele, order })
                    flag = true
                    break
                }
            }
            // 表示当前的order都比前面的大直接插入到最后
            if (!flag) {
                this.enqueue({ ele, order })
            }
        }

    }

}

export { Queue, PriorityQueue }